home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / MATHS / RLAB / RLAB125.ZIP / !RLaB / rlib / diff < prev    next >
Text File  |  1994-09-20  |  786b  |  39 lines

  1. //-------------------------------------------------------------------//
  2.  
  3. //  Syntax:    diff ( X )
  4. //        diff ( X , k )
  5.  
  6. //  Description:
  7.  
  8. //  The function diff computes the difference between adjacent
  9. //  elements in a vector. The function diff merely does:
  10.  
  11. //    x = x[2:n] - x[1:n-1];
  12.  
  13. //  If X is a matrix, then the operation is performed on each column
  14. //  of X.
  15.  
  16. //  The default value for `k' is 1. If k is specified as other than 1,
  17. //  then the operation is performed k times.
  18.  
  19. //-------------------------------------------------------------------//
  20.  
  21. diff = function ( X, k )
  22. {
  23.   local (X, k)
  24.  
  25.   if (!exist (k)) { k = 1; }
  26.  
  27.   for (i in 1:k)
  28.   {
  29.     m = X.nr; n = X.nc;
  30.     if (m == 1)
  31.     {
  32.       X = X[2:n] - X[1:n-1];
  33.     else
  34.       X = X[2:m;] - X[1:m-1;];
  35.     }
  36.   }
  37.   return X;
  38. };
  39.